from lib.tencent.translate import Translate as tencent_translate
import re


class Translate:

    @classmethod
    def INPUT_TYPES(s):
        return {
            "required": {
                "content": ("STRING", {"multiline": True, "default": ""}),
                "driver": (
                    [
                        "tencent",
                    ],
                    {"default": "tencent"},
                ),
                "remove_s": ("BOOLEAN", {"default": True}),
                
            }
        }

    RETURN_TYPES = (
        "STRING",
        "STRING",
    )
    RETURN_NAMES = (
        "target",
        "source",
    )

    FUNCTION = "handle"

    OUTPUT_NODE = False

    CATEGORY = "fcl/translate"

    history_dict = {}

    def handle(self, content, driver, remove_s = False):
        resp = ""
        if driver == "tencent":
            if (
                self.history_dict.get("driver", "") != driver
                or self.history_dict.get("content", "") != content
            ):
                tt = tencent_translate()
                resp = tt.trans(content)
                if remove_s:
                    resp = re.sub(r',\s*', ',', resp)
                    
                self.history_dict = {
                    "driver": driver,
                    "content": content,
                    "resp": resp,
                }
                print(self.history_dict, "-0---")
            resp = self.history_dict.get("resp", "")

        return resp, content


NODE_CLASS_MAPPINGS = {"FCL_TRANSLATE": Translate}

NODE_DISPLAY_NAME_MAPPINGS = {"FCL_TRANSLATE": "fcl dev translate"}
